<%@page import="java.util.Date"%>
<%@page import="com.web.entity.Student"%>
<%@page errorPage="SloginerrorPage.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Student Message</title>
    <body>
        <div id="navdiv">
            <div class="top_navdiv">
                <div class="top_left">学生中心</div>
                <div class="identity">
                    <span>您好</span>
                    <div class="user_li">
                        <canvas id="myCanvas" width="100" height="30">
                        </canvas>
                    </div>
                </div>
            </div>
        </div>
        <div class="navbar  control_nav" >
            <div class="container"  style="margin-left: 300px; ">
                <ul class="nav navbar-nav navul" >
                    <li class="col-md-2">
                        <a href="startTest.jsp">开始考试</a>
                    </li>
                </ul>
            </div>
        </div>
        <link rel="stylesheet" href="css/bootstrap.min.css"/>
        <link rel="stylesheet" href="css/newcss1.css">
        <link rel="stylesheet" href="css/mycss_foradim.css"/>
        <script src="js/jquery-3.4.1.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/script.js" charset="UTF-8"></script> 
        <script type="text/javascript" src="js/circle.js" charset="UTF-8"></script>

        <hr>


        <%
            Student s = (Student) session.getAttribute("s");
        %>

        <div class="container">
            <div class="col-md-3 main_div nav_list">
                <span><b>学生</b></span>
                <div class="div_line"></div>
                <ul>
                    <li>
                        <img class="img-circle" width="74px" height="70px;" src="img/login_img.jpg" alt="图片加载失败">
                    </li>
                    <span class="glyphicon glyphicon-chevron-right"></span><a href="studentlogin.html"><span>返回登录页面</span></a>
                </ul>
            </div>
            <div class="col-md-1"></div>
            <div class="col-md-8 main_div">
                <table id="dataTable" class="table-condensed table-hover test_table table-striped">
                    <tr class="table-hover">
                        <th class="text-center">学号信息</th>
                        <th class="text-center">学生姓名</th>
                        <th class="text-center">性别</th>
                        <th class="text-center">所在班级</th>
                        <th class="text-center">操作</th>
                    </tr>

                    <tr class="forEach_tr" id="tr<%=s.getSid()%>">
                        <td class="col-md-2 text-center"><%=s.getSid()%></td>
                        <td class="col-md-2 text-center"><%=s.getSname()%></td>
                        <td class="col-md-2 text-center"><%=s.getSgender()%></td>
                        <td class="col-md-2 text-center"><%=s.getClassID()%></td>
                        <td class="text-center"> <a href="javascript:showsModifyModal('#tr<%=s.getSid()%>');" class="text-success">修改个人信息</a><br>
                            <a href="javascript:showModifySpwdModal('#tr<%=s.getSid()%>');" class="text-danger">修改密码</a>
                        </td>
                    </tr>
                </table>

            </div>
        </div>
        <div class="modal fade" id="modify_smodal" tabindex="-1">
            <div class="modal-dialog">
                <div class="bg-warn modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title text-primary" id="myModalLabel">编辑学生信息</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-inline" id="smodify_form">
                            <label for="sid">学生学号：</label>
                            <input id="sid" type="text" name="Sid" readonly class="form-control">
                            <br><br>
                            <label for="iname">学生姓名：</label>
                            <input id="iname" type="text" name="Sname" class="form-control">
                            <br><br>
                            <label for="igender">学生性别：</label>
                            <select id="igender" name="Sgender" class="form-control">
                                <option value="男">男</option>
                                <option value="女">女</option>
                            </select>
                            <br><br>
                            <label for="iclass">所在班级:</label>
                            <select id="iclass" name="ClassID" class="form-control">
                                <option value="2018001">2018001</option>
                                <option value="2018002">2018002</option>
                                <option value="2018003">2018003</option>
                                <option value="2018004">2018004</option>
                            </select>
                            <br><br>

                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-info" data-dismiss="modal">取消</button>
                        <button type="button" id="btn_smodify" class="btn btn-primary">保存修改</button>
                    </div>
                </div>
            </div>
        </div>


        <div class="modal fade" id="modifyspwd_modal" tabindex="-1">
            <div class="modal-dialog">
                <div class="bg-warn modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title text-primary" id="myModalLabel">修改登录密码</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-inline" id="modifypwd_form" action="action" class="container">
                            <label for="sid">学 &nbsp;生  &nbsp;学  &nbsp;号&nbsp;:&nbsp;&nbsp;&nbsp;</label>
                            <input id="sid" type="text" name="Sid" readonly class="form-control">
                            <br><br>
                            <label for="spwd1">请输入原密码：</label>
                            <input id="spwd1" type="text" name="NSpwd" class="form-control">
                            <br><br>
                            <label for="spwd">请输入新密码：</label>
                            <input id="spwd" type="text" name="Spwd" class="form-control">
                            <br><br>
                            <label for="nspwd1">请确认新密码：</label>
                            <input id="nspwd1" type="text" name="NSpwd1" class="form-control">
                            <br><br>
                            <input type="hidden" name="sid" id="tid" value="{$Sid}">
                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-info" data-dismiss="modal">取消</button>
                        <button type="button" id="btn_modifySpwd" class="btn btn-primary">保存密码</button>
                    </div>
                </div>
            </div>
        </div>


        <script>

            $(document).ready(function () {

//点击“保存修改”按钮，发送请求给msstu这个Servlet，修改数据库中对应的信息
                $("#btn_smodify").click(function () {
                    $.ajax({
                        url: "msstu",
                        type: "POST",
                        data: $("#smodify_form").serialize(),
                        success: function (rows) {
                            if (rows != 1) {
                                alert("修改失败，请确保格式正确！");
                                return;
                            }
                            var rowId = "#tr" + $("#smodify_form #sid").val();
                            $(rowId + " td:eq(1)").text($("#smodify_form #iname").val());
                            $(rowId + " td:eq(2)").text($("#smodify_form #igender").val());
                            $(rowId + " td:eq(3)").text($("#smodify_form #iclass").val());
                            $("#modify_smodal").modal("hide");
                        },
                        error: function () {
                            alert("请求处理出错，修改失败！");
                        }
                    });
                });
            });



//点击“编辑”超链接执行的函数，弹出用于编辑信息的模态框
            function showsModifyModal(rowId) {
                $("#modify_smodal #sid").val($(rowId + " td:eq(0)").text());
                $("#modify_smodal #iname").val($(rowId + " td:eq(1)").text());
                $("#modify_smodal #igender").val($(rowId + " td:eq(2)").text());
                $("#modify_smodal #iclass").val($(rowId + " td:eq(3)").text());
                $("#modify_smodal").modal("show");
            }

//点击“修改密码”超链接执行的函数，弹出用于编辑信息的模态框
            function showModifySpwdModal(rowId) {
                $("#modifyspwd_modal #sid").val($(rowId + " td:eq(0)").text());
                $("#modifyspwd_modal").modal("show");
            }


            $(document).ready(function () {
                $("#btn_modifySpwd").click(function () {
                    $.ajax({
                        url: "msps",
                        type: "POST",
                        data: $("#modifypwd_form").serialize(),
                        success: function (rows) {
                            if (rows != 1) {
                                alert("修改失败！");
                                return;
                            } else
                            if ($('#spwd1').val() == "") {
                                alert('原始密码为空！');
                                return;
                            } else

                            if ($('#spwd1').val() !=<%=s.getSpwd()%>) {
                                alert('原密码输入错误！');
                            } else

                            if ($('#spwd').val() == "") {
                                alert('新密码为空！');
                                return;
                            } else

                            if ($('#nspwd1').val() == "") {
                                alert('确认密码为空！');
                                return;
                            } else

                            if ($('#nspwd1').val() != $('#spwd').val()) {
                                alert('两次输入密码不一致！');
                                return;
                            } else

                            if ($('#spwd').val() == $('#spwd1').val()) {
                                alert('新密码不能与原密码相同！');
                                return;
                            } else

                            if ($('#nspwd1').val() == $('#spwd1').val()) {
                                alert('新密码不能与原密码相同！');
                                return;
                            } else
                            {
                                var rowId = "#tr" + $("#modifypwd_form #sid").val();
                                $(rowId + " td:eq(6)").text($("#modifypwd_form #spwd").val());
                                $("#modifyspwd_modal").modal("hide");
                                alert('您已修改密码，请重新登录！');
                                window.location.href = 'studentlogin.html?rowId' + rowId;
                            }
                        },
                        error: function () {
                            alert("请求处理出错，修改失败！");
                        }
                    });
                });
            });
        </script>              
    </body>
</html>
